#include "asmgen.hpp"
#include "ast.hpp"
#include "ir.hpp"
#include <cassert>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>

using namespace std;

extern FILE* yyin;
extern int   yyparse(unique_ptr<BaseAST>& ast);

unique_ptr<string> to_koopa_text(unique_ptr<BaseAST>& ast);

int main(int argc, char** argv)
{
    assert(argc == 5);

    const string mode{argv[1]};

    auto input      = argv[2];
    auto outputfile = argv[4];

    yyin = fopen(input, "r");
    assert(yyin);

    unique_ptr<BaseAST> ast;
    auto                ret = yyparse(ast);
    assert(!ret);

    cout << endl;

    unique_ptr<string> output{to_koopa_text(ast)};
    cout << *output << endl;
    if (mode == "-koopa") {
        //
    }
    else if (mode == "-riscv") {
        output = to_riscv32_asm(output);
    }

    ofstream outfile{outputfile};
    outfile << *output << endl;

    return 0;
}
